#include "LinkTree.h"
#include <iostream>

// 带参构造
TreeNode::TreeNode(const ElementType &element):data(element)
{
    parent = FirstChild = nextSbling = nullptr; //开始时全指向空
}

// 节点连接
void TreeNode::Connect(TreeNode *parent)
{
    if(!parent)
        return;
    this->parent = parent;
    // 头插（链表头插快）
    this->nextSbling = parent->FirstChild;
    parent->FirstChild = this;
}

// 解绑
void TreeNode::DisConnect(TreeNode *parent)
{
    if(parent == nullptr || this->parent != parent)
        return;
    // 把节点从双亲节点的孩子集合中删除
    TreeNode *travelPoint = parent->FirstChild;
    if(this == parent->FirstChild)
    {
        parent->FirstChild = this->nextSbling;
    }
    else
    {
        while(travelPoint->nextSbling != nullptr)
        {
            if(travelPoint->nextSbling == this)
            {
                travelPoint->nextSbling = this->nextSbling;
                break;
            }
            travelPoint = travelPoint->nextSbling;
        }
    }
    parent = nextSbling = nullptr;
}

// 找节点
TreeNode *TreeNode::FindNode(TreeNode *node, const ElementType &element)
{
    // 递归终止条件
    if(node == nullptr)
        return nullptr;
    // 排除根节点
    if(node->data == element && node->parent != nullptr)
        return node;
    // 在孩子里找
    TreeNode *target = FindNode(node->FirstChild, element);
    if(target)
        return target;
    // 在兄弟里找
    target = FindNode(node->nextSbling, element);
    if(target)
        return target;
    // 都找不到
    return nullptr;
}

// 找节点
void TreeNode::FindNode(TreeNode *node, const ElementType &element, DARRAY::DynamicArray &a)
{
    // 终止条件
    if(node == nullptr)
        return;
    // 排除根节点
    if(node->data == element && node->parent != nullptr)
    {
        a.InsertTail(node);
    }
    FindNode(node->FirstChild, element, a);
    FindNode(node->nextSbling, element, a);
}

// 释放节点
void TreeNode::FreeNode(TreeNode *node)
{
    // 递归终止条件
    if(node == nullptr)
        return;
    FreeNode(node->FirstChild);
    FreeNode(node->nextSbling);
    delete node;
}

// 获取节点高度
int TreeNode::GetNodeHeight(TreeNode *node)
{
    // 节点为空，树高为0
    if(node == nullptr)
        return 0;
    int MaxHeight = 0;
    // 找最高的子树
    TreeNode *travelPoint = node->FirstChild;
    while(travelPoint != nullptr)
    {
        int height = GetNodeHeight(travelPoint);
        MaxHeight = MaxHeight > height ? MaxHeight : height;
        travelPoint = travelPoint->nextSbling;
    }
    // 自身高度 = 最高子树高度 + 1
    return MaxHeight + 1;
}

// show
void TreeNode::show(TreeNode *node, int depth)
{
    // 递归终止条件
    if(node == nullptr) 
        return;
    // 根据深度打印空格
    for(int i = 0; i < depth; i++)
        std::cout<<" ";
    if(depth > 0)
        std::cout<<"|_";
    // 排除根节点打印
    if(node->parent != nullptr)
        std::cout<<node->data<<std::endl;
    // DFS：深度优先遍历（先访问孩子，再访问兄弟）
    show(node->FirstChild, depth+1);
    show(node->nextSbling, depth);
}

// 获取双亲指针
TreeNode *TreeNode::GetParent()
{
    return parent;
}

// 获取数据域
std::string &TreeNode::GetData()
{
    return data;
}

void TreeNode::SetData(const ElementType &element)
{
    data = element;
}

// 无参构造 
LinkTree::LinkTree()
{
    root = new TreeNode("");
}

// 带参构造
LinkTree::LinkTree(const ElementType &element)
{
    root = new TreeNode(element);
}

// 析构
LinkTree::~LinkTree()
{
    TreeNode::FreeNode(root);
}

// 获取根节点地址
TreeNode *LinkTree::GetRoot()
{
    return root;
}

// show
void LinkTree::show()
{
    root->show(root, -1);
}

// 找节点
DARRAY::DynamicArray LinkTree::FindByElement(const ElementType &element)
{
    DARRAY::DynamicArray a;
    TreeNode::FindNode(root, element, a);
    return a;
}

// 获取树的高度
int LinkTree::GetTreeHeight()
{
    return TreeNode::GetNodeHeight(root) - 1;
}

// 获取节点路径
std::string LinkTree::GetNodePath(TreeNode *node)
{
    if(node == nullptr)
        return std::string();
        
    std::string path = node->GetData();
    while(node->GetParent() != nullptr)
    {
        path = node->GetParent()->GetData() + "/" + path;
        node = node->GetParent();
    }
    return path;
}
